/*
 * Copyright (c) 2011 Jean-Marc Perronne
 * Ensisa, 12 rue des Freres Lumieres, F-68093 Mulhouse cedex
 * All rights reserved.
 *
 * The copyright of this software is owned by Werner Randelshofer. 
 * You may not use, copy or modify this software, except in  
 * accordance with the license agreement you entered into with  
 * Jean-Marc Perronne. For details see accompanying license terms. 
 */

package fr.uha.ensisa.jmp.drone.navdata;

public class DroneState
{
	private static final int ARDRONE_FLY_MASK            	= 1 << 0;  /*!< FLY MASK : (0) ardrone is landed, (1) ardrone is flying */
	private static final int ARDRONE_VIDEO_MASK          	= 1 << 1;  /*!< VIDEO MASK : (0) video disable, (1) video enable */
	private static final int ARDRONE_VISION_MASK        	= 1 << 2;  /*!< VISION MASK : (0) vision disable, (1) vision enable */
	private static final int ARDRONE_CONTROL_MASK       	= 1 << 3;  /*!< CONTROL ALGO : (0) euler angles control, (1) angular speed control */
	private static final int ARDRONE_ALTITUDE_MASK      	= 1 << 4;  /*!< ALTITUDE CONTROL ALGO : (0) altitude control inactive (1) altitude control active */
	private static final int ARDRONE_USER_FEEDBACK_START 	= 1 << 5;  /*!< USER feedback : Start button state */
	private static final int ARDRONE_COMMAND_MASK			= 1 << 6;  /*!< Control command ACK : (0) None, (1) one received */
	//private static final int ARDRONE_FW_FILE_MASK			= 1 << 7;  /* Firmware file is good (1) */
	//private static final int ARDRONE_FW_VER_MASK			= 1 << 8;  /* Firmware update is newer (1) */
	//private static final int ARDRONE_FW_UPD_MASK			= 1 << 9;  /* Firmware update is ongoing (1) */
	private static final int ARDRONE_NAVDATA_DEMO_MASK   	= 1 << 10; /*!< Navdata demo : (0) All navdata, (1) only navdata demo */
	private static final int ARDRONE_NAVDATA_BOOTSTRAP		= 1 << 11; /*!< Navdata bootstrap : (0) options sent in all or demo mode, (1) no navdata options sent */	
	private static final int ARDRONE_MOTORS_MASK 			= 1 << 12; /*!< Motors status : (0) Ok, (1) Motors problem */
	private static final int ARDRONE_COM_LOST_MASK			= 1 << 13; /*!< Communication Lost : (1) com problem, (0) Com is ok */
	private static final int ARDRONE_GYROS_ZERO			= 1 << 14; /*!< Gyro PB : (1) , (0) Ok */
	private static final int ARDRONE_VBAT_LOW				= 1 << 15; /*!< VBat low : (1) too low, (0) Ok */
	//private static final int ARDRONE_USER_EL 				= 1 << 16; /*!< User Emergency Landing : (1) User EL is ON, (0) User EL is OFF*/
	private static final int ARDRONE_TIMER_ELAPSED			= 1 << 17; /*!< Timer elapsed : (1) elapsed, (0) not elapsed */
	private static final int ARDRONE_NOT_ENOUGH_POWER		= 1 << 18; /*!< Angles : (0) Ok, (1) out of range */
	private static final int ARDRONE_ANGLES_OUT_OF_RANGE	= 1 << 19; /*!< not enough power */
	private static final int ARDRONE_WIND_MASK				= 1 << 20; /*!< too much wind */
	private static final int ARDRONE_ULTRASOUND_MASK		= 1 << 21; /*!< Ultrasonic sensor : (0) Ok, (1) deaf */
	//private static final int ARDRONE_CUTOUT_MASK 			= 1 << 22; /*!< Cutout system detection : (0) Not detected, (1) detected */
	//private static final int ARDRONE_PIC_VERSION_MASK		= 1 << 23; /*!< PIC Version number OK : (0) a bad version number, (1) version number is OK */
	private static final int ARDRONE_ATCODEC_THREAD_ON		= 1 << 24; /*!< ATCodec thread ON : (0) thread OFF (1) thread ON */
	private static final int ARDRONE_NAVDATA_THREAD_ON		= 1 << 25; /*!< Navdata thread ON : (0) thread OFF (1) thread ON */
	private static final int ARDRONE_VIDEO_THREAD_ON		= 1 << 26; /*!< Video thread ON : (0) thread OFF (1) thread ON */
	//private static final int ARDRONE_ACQ_THREAD_ON			= 1 << 27; /*!< Acquisition thread ON : (0) thread OFF (1) thread ON */
	//private static final int ARDRONE_CTRL_WATCHDOG_MASK		= 1 << 28; /*!< CTRL watchdog : (1) delay in control execution (> 5ms), (0) control is well scheduled */
	//private static final int ARDRONE_ADC_WATCHDOG_MASK 		= 1 << 29; /*!< ADC Watchdog : (1) delay in uart2 dsr (> 5ms), (0) uart2 is good */
	private static final int ARDRONE_COM_WATCHDOG_MASK  	= 1 << 30; /*!< Communication Watchdog : (1) com problem, (0) Com is ok */
	private static final int ARDRONE_EMERGENCY_MASK      	= 1 << 31;  /*!< Emergency landing : (0) no emergency, (1) emergency */

	public static boolean isFlying(int state)
	{
		return testMask(state, ARDRONE_FLY_MASK);
	}

	public static boolean isVideoEnabled(int state)
	{
		return testMask(state, ARDRONE_VIDEO_MASK);
	}

	public static boolean isVisionEnabled(int state)
	{
		return testMask(state, ARDRONE_VISION_MASK);
	}

	public static boolean IsAngulerSpeedControlEnabled(int state)
	{
		return testMask(state, ARDRONE_CONTROL_MASK);
	}

	public static boolean IsAltitudeControlEnabled(int state)
	{
		return testMask(state, ARDRONE_ALTITUDE_MASK);
	}

	public static boolean IsStartButtonEnabled(int state)
	{
		return testMask(state, ARDRONE_USER_FEEDBACK_START);
	}

	public static boolean isCommandProcessing(int state)
	{
		return testMask(state, ARDRONE_COMMAND_MASK);
	}

	public static boolean isInNavDemoMode(int state)
	{
		return testMask(state, ARDRONE_NAVDATA_DEMO_MASK);
	}

	public static boolean isInBootstrapMode(int state)
	{
		return testMask(state, ARDRONE_NAVDATA_BOOTSTRAP);
	}

	public static boolean isComWatchdogEnabled(int state)
	{
		return testMask(state, ARDRONE_COM_WATCHDOG_MASK);
	}

	public static boolean hasMotorsProblemt(int state)
	{
		return testMask(state, ARDRONE_MOTORS_MASK);
	}

	public static boolean isCommunicationLost(int state)
	{
		return testMask(state, ARDRONE_COM_LOST_MASK);
	}

	public static boolean HasGyroProblem(int state)
	{
		return testMask(state, ARDRONE_GYROS_ZERO);
	}

	public static boolean isBatteryLow(int state)
	{
		return testMask(state, ARDRONE_VBAT_LOW);
	}

	public static boolean hasTimerElapsed(int state)
	{
		return testMask(state, ARDRONE_TIMER_ELAPSED);
	}

	public static boolean areAnglesOutOfRange(int state)
	{
		return testMask(state, ARDRONE_ANGLES_OUT_OF_RANGE);
	}

	public static boolean hasNotEnoughPower(int state)
	{
		return testMask(state, ARDRONE_NOT_ENOUGH_POWER);
	}

	public static boolean tooMuchWind(int state)
	{
		return testMask(state, ARDRONE_WIND_MASK);
	}

	public static boolean hasUltraSoundProblem(int state)
	{
		return testMask(state, ARDRONE_ULTRASOUND_MASK);
	}

	public static boolean HasActiveATCommandThread(int state)
	{
		return testMask(state, ARDRONE_ATCODEC_THREAD_ON);
	}

	public static boolean HasActiveNavigationDataThread(int state)
	{
		return testMask(state, ARDRONE_NAVDATA_THREAD_ON);
	}

	public static boolean HasActiveVideoStreamThread(int state)
	{
		return testMask(state, ARDRONE_VIDEO_THREAD_ON);
	}

	public static boolean HasCommunicationProblem(int state)
	{
		return testMask(state, ARDRONE_COM_WATCHDOG_MASK);
	}

	public static boolean hasEmergency(int state)
	{
		return testMask(state, ARDRONE_EMERGENCY_MASK);
	}

	protected static boolean testMask(int state, int mask)
	{
		return (state & mask) == mask;
	}

}
